﻿// <copyright file="TTableStorageDataContextTest.cs" company="N/A">
//     Copyright (c) Nicholas Barrett and Benjamin Kasten.  All rights reserved.
//
//     This program is free software; you can redistribute it and/or modify it
//     under the terms of the GNU General Public License as published by the
//     Free Software Foundation; either version 2 of the License, or (at your
//     option) any later version.
//
//     This program is distributed in the hope that it will be useful, but
//     WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
//     Public License for more details.
//
//     You should have received a copy of the GNU General Public License along
//     with this program; if not, write to the Free Software Foundation, 
//     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// </copyright>

namespace BetterAzureTableStorage.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;

    /// <summary>
    /// Contains tests for TTableStorageDataContext
    /// </summary>
    [TestClass]
    public class TTableStorageDataContextTest
    {
        /// <summary>
        /// Creates a new TTableStorageDataContext and accessor.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="accessor">The accessor.</param>
        private static void CreateNewContext(out TTableStorageDataContext context, out TTableStorageDataContext_Accessor accessor)
        {
            context = new TTableStorageDataContext(
                CloudStorageAccount.DevelopmentStorageAccount.TableEndpoint.ToString(),
                CloudStorageAccount.DevelopmentStorageAccount.Credentials,
                null);

            accessor = new TTableStorageDataContext_Accessor(new PrivateObject(context));
        }

        [TestMethod]
        public void CreateTablesIfNotExistsTest()
        {
            TTableStorageDataContext context;
            TTableStorageDataContext_Accessor accessor;
            CreateNewContext(out context, out accessor);

            Random rng = new Random();

            string tableName = String.Format("test{0}{0}", rng.Next().ToString());
            context.CreateTableIfNotExists(tableName);
            Assert.IsTrue(TTableStorageDataContext_Accessor.fCreatedTables[accessor.ServiceRoot].Contains(tableName));
        }
    }
}
